using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Recruiters_contactedresume : System.Web.UI.Page
{
    void LoadDataForDropDownList()
    {
        Common cls = new Common(connectionString);
        DataTable dataTable = cls.GetDataTable(
       "select * from tblRCJobPostings "+
       " where Activate=1 and RecruiterID=" +
       Session["rcuserid"].ToString());
       DataRow dataRow = dataTable.NewRow();
       dataRow["JobID"] = "0";
       dataRow["JobTitle"] = "Select on JobTile";
       dataTable.Rows.InsertAt(dataRow,0);
        DropDownListHelper helper =
        new DropDownListHelper();
        helper.FillDataTableToDropDownList(
        dataTable, ddlJob, 
        "JobTitle", "JobID", "0");
        if(dataTable.Rows.Count == 0)
            btnSend.Enabled = false;
        
    }
    void  ViewContact(string jobID, string resumeID)
    {
        SqlServerProvider cls = new SqlServerProvider(connectionString);
        cls.CommandText = "SPViewContact";
        cls.CommandType = CommandType.StoredProcedure;
        cls.ParameterCollection = new 
        string [3] {"@RecruiterID", 
        "@JobID", "@ResumeID"};
        cls.ValueCollection = new string[3] 
        { Session["rcuserid"].ToString(), 
        jobID, resumeID };
        DataRow dataRow = cls.GetDataRow();
        if( dataRow != null)
        {
            literalResumeID.Text = resumeID;
            Literal1.Text = dataRow["JobPosition"].ToString();
            Literal2.Text = dataRow["Email"].ToString();
            Literal3.Text = dataRow["FullName"].ToString();
            if (dataRow["JobStatus"].ToString() != "")
               ddlJob.SelectedValue = jobID;            
            txtName.Text = dataRow["ContactName"].ToString();
            txtSubject.Text = dataRow["ContactSubject"].ToString();
            txtBody.Text = dataRow["ContactBody"].ToString();
            btnSend.Text = "Resend";
        }
    }
    void EditLetter()
    {        
        Page page = this.PreviousPage;
        if (page != null)
        {
            Literal literal = (Literal)
                page.FindControl("literalResume");
            literalResumeID.Text = literal.Text;

            literal = (Literal)
            page.FindControl("literalJob");
            ddlJob.SelectedValue = literal.Text;

            literal = (Literal)
            page.FindControl("Literal0");
            Literal1.Text = literal.Text;

            literal = (Literal)
            page.FindControl("Literal11");
            Literal3.Text = literal.Text;

            literal = (Literal)
            page.FindControl("Literal12");
            Literal3.Text += " " + literal.Text;

            literal = (Literal)
           page.FindControl("Literal6");
            Literal2.Text += " " + literal.Text;
        }
        else
        {
            string jobID = "", resumeID = "";
            jobID = Request.QueryString.Get("JobID");
            resumeID = Request.QueryString.Get("ResumeID");
            if (jobID == null || jobID == "") jobID = "0";
            ViewContact(jobID, resumeID);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    
        if (Session["rcuserid"] == null)
            Response.Redirect("login.aspx");
        try
            {
             connectionString =
             WebConfigurationManager.ConnectionStrings[
               "RecruitVietnam"].ConnectionString;
             
             if(!IsPostBack)
             {
                 LoadDataForDropDownList();
                 EditLetter();
                
            }
         }
         catch(Exception ex)
         {
            literalError.Text = ex.Message;
         }
    }
    string connectionString = "";
    protected void btnSend_Click(object sender, EventArgs e)
    {
        
        RecruitVietnam cls = new RecruitVietnam();
        cls.ConnectionString = connectionString;
        if(!cls.SaveContactCandidate(
        Session["rcuserid"].ToString(),
        ddlJob.SelectedValue,
        literalResumeID.Text,
        txtName.Text,
        txtSubject.Text,
        txtBody.Text))
            literalError.Text = cls.ErrorMessage;
        else
        {
            MailProvider mailProvider = new MailProvider();
            mailProvider.SendMail(Literal2.Text, txtSubject.Text,
            txtBody.Text + "<br>" + txtName.Text);
            Response.Redirect("contactedresumes.aspx");
        }
    }
}
